//
//  EntryProtectTargetPositionVC.m
//  MyProject
//
//  Created by 石小强 on 2018/4/24.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "EntryProtectTargetPositionVC.h"
#import "EntryProtectTargetPositionCell.h"
#import "EntryProtectTargetPositionBottomView.h"
#import "PositionModel.h"
#import "SearchCompanyViewController.h"
#import "CompanyModel.h"
#import "SalarySelectViewController.h"
#import "UITableView+Empty.h"

@interface EntryProtectTargetPositionVC ()<EntryProtectTargetPositionCellDelegate>

@property (nonatomic, strong) EntryProtectTargetPositionBottomView *bottomView;
@property (nonatomic, strong) NSMutableArray *positionArray;

@property (nonatomic, strong) CompanyModel *selectCompany;

@property (nonatomic, copy) NSString *minSalary;
@property (nonatomic, copy) NSString *maxSalary;

@end

@implementation EntryProtectTargetPositionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNav];
    
    [self setupTableView];
    
    [self getPositionListByCompanyId];
}

- (void)setupNav {
    self.navigationItem.title = @"目标职位";
    CGSize saveSize = [Tool sizeWithText:@"保存" font:[UIFont fontWithName:@"PingFangSC-Regular" size:14]];
    UIButton *saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, saveSize.width, 44)];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn setTitleColor:UIColorFromRGB(0x373B3C) forState:UIControlStateNormal];
    saveBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
    [saveBtn addTarget:self action:@selector(saveBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:saveBtn];
}

- (void)saveBtnClick {
    if (self.bottomView.selectBtn.selected) {  // 选择的是其他目标职位
        if (!self.bottomView.positionNameT.text || self.bottomView.positionNameT.text.length == 0) {
            [CommonUtils showToast:@"请输入职位名称"];
            return;
        }
        if (!self.bottomView.companyNameL.text || self.bottomView.companyNameL.text.length == 0) {
            [CommonUtils showToast:@"请选择公司名称"];
            return;
        }
        if (!self.bottomView.salaryL.text || self.bottomView.salaryL.text.length == 0) {
            [CommonUtils showToast:@"请选择职位薪资"];
            return;
        }
//        NSString *salaryStr = self.bottomView.positionSalaryT.text;
//        NSArray *salaryArr = [salaryStr componentsSeparatedByString:@"-"];
//        if (salaryArr.count != 2) {
//            [CommonUtils showToast:@"请用-区分出薪资范围"];
//            return;
//        }
        
        self.okBlock(@"", self.bottomView.positionNameT.text, self.bottomView.companyNameL.text, self.minSalary, self.maxSalary);
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        PositionModel *selectModel = nil;
        for (PositionModel *model in self.positionArray) {
            if (model.isSelect == 1) {
                selectModel = model;
                break;
            }
        }
        if (!selectModel) {
            [CommonUtils showToast:@"请选择职位"];
            return;
        } else {
            self.okBlock(selectModel.positionId, selectModel.positionName, selectModel.companyAbbreviatedName, selectModel.minSalary, selectModel.maxSalary);
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

- (void)setupTableView {
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([EntryProtectTargetPositionCell class]) bundle:nil] forCellReuseIdentifier:@"EntryProtectTargetPositionCell"];
    
    self.bottomView = [[[NSBundle mainBundle] loadNibNamed:@"EntryProtectTargetPositionBottomView" owner:self options:nil] lastObject];
    self.bottomView.frame = CGRectMake(0, 0, kScreenWidth, 238);
    self.tableView.tableFooterView = self.bottomView;
    
    [self.bottomView.selectBtn addTarget:self action:@selector(bottomViewSelectBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bottomView.companyNameB addTarget:self action:@selector(bottomViewCompanyBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bottomView.salaryBtn addTarget:self action:@selector(bottomViewSalaryBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

/** 底部选择按钮 */
- (void)bottomViewSelectBtnClick {
    self.bottomView.selectBtn.selected = !self.bottomView.selectBtn.selected;
    
    if (self.bottomView.selectBtn.selected) {
        for (PositionModel *model in self.positionArray) {
            model.isSelect = 0;
        }
        
        [self.tableView reloadData];
    }
}

/** 底部选择公司名称按钮 */
- (void)bottomViewCompanyBtnClick {
    SearchCompanyViewController *scVC = [[SearchCompanyViewController alloc] init];
    scVC.index = 1;
    scVC.companyBlock = ^(CompanyModel *company) {
        self.selectCompany = company;
        self.bottomView.companyNameL.text = company.companyAbbreviatedName;
    };
    [self.navigationController pushViewController:scVC animated:YES];
}

- (void)bottomViewSalaryBtnClick {
    SalarySelectViewController *salary = [[SalarySelectViewController alloc] init];
    WS(weakSelf)
    salary.selectBlock = ^(NSString *minSalary, NSString *maxSalary) {
        weakSelf.minSalary = [minSalary substringToIndex:minSalary.length - 1];
        weakSelf.maxSalary = [maxSalary substringToIndex:maxSalary.length - 1];
        weakSelf.bottomView.salaryL.text = [NSString stringWithFormat:@"%@-%@", minSalary, maxSalary];
    };
    // 核心代码
    self.definesPresentationContext = YES;
    salary.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [self.navigationController presentViewController:salary animated:NO completion:nil];
}

/** 查询企业职位列表 */
- (void)getPositionListByCompanyId {
    WS(weakSelf)
    [self showProgressWithView:self.view animated:YES];
    
    [SendRequest getPostionListByCompanyId:self.companyId andPageNo:@"1" andJobId:@"" result:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:self.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"查询企业职位列表 = %@", [[result objectForKey:@"data"] objectForKey:@"list"]);
            
            NSArray *datas = [PositionModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"data"] objectForKey:@"list"]];
            
//            if (datas.count == 0) {
//                [weakSelf.tableView noDataAddEmptyUIWithImageName:@"pic_emptystate_two" andEmptyText:@"暂无相关数据" andTopDistance:130];
//                self.tableView.tableFooterView = nil;
//            } else {
//                [weakSelf.tableView haveDataRemoveEmptyUI];
//                self.tableView.tableFooterView = self.bottomView;
//            }
            
            [weakSelf.positionArray removeAllObjects];
            [weakSelf.positionArray addObjectsFromArray:datas];
            [weakSelf.tableView reloadData];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

#pragma mark - EntryProtectTargetPositionCellDelegate

- (void)selectBtnClickWithModel:(PositionModel *)model andIndex:(NSInteger)index andIsSelect:(NSInteger)isSelect {
    if (isSelect == 1) {
        self.bottomView.selectBtn.selected = NO;
        
        for (PositionModel *position in self.positionArray) {
            if (![position.positionId isEqualToString:model.positionId]) {
                position.isSelect = 0;
            }
        }
        [self.tableView reloadData];
    }
}

#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.positionArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (self.positionArray.count > 0) {
        return 30;
    } else {
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (self.positionArray.count > 0) {
        UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
        header.backgroundColor = kStyleColor;
        
        UILabel *contentL = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 200, 30)];
        contentL.text = @"推荐职位";
        contentL.font = [UIFont fontWithName:@"PingFangSC-Regular" size:11];
        contentL.textColor = UIColorFromRGB(0x859BA6);
        [header addSubview:contentL];
        
        return header;
    } else {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 91;
    } else {
        return 91 + 15;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EntryProtectTargetPositionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EntryProtectTargetPositionCell"];
    cell.model = [self.positionArray objectAtIndex:indexPath.row];
    cell.index = indexPath.row;
    cell.delegate = self;
    return cell;
}

- (NSMutableArray *)positionArray {
    if (!_positionArray) {
        _positionArray = [[NSMutableArray alloc] init];
    }
    return _positionArray;
}

@end
